//
//  Home.swift
//  ChinaLandmark
//
//  Created by iGhibli on 2020/12/23.
//

import SwiftUI

struct Home: View {
    
    @State var isPresented = false
    
    var categories: [String: [Landmark]] {
        Dictionary(grouping: landmarks, by: { $0.category })
    }

    var body: some View {
        NavigationView {
            List {
//                Image(landmarks[0].imageName)
//                    .resizable()
//                    .scaledToFill()
//                    .frame(height: 200)
//                    .clipped()
//                    .listRowInsets(EdgeInsets())
                PageVC()
                    .frame(height: 200)
                    .listRowInsets(EdgeInsets())
                
                ForEach(categories.keys.sorted(), id: \.self) { categoryName in
                    CategoryCell(
                        categoryName: categoryName,
                        landmarks: categories[categoryName]!)
                }
                .listRowInsets(EdgeInsets()) // 取消cell默认边距
                
                NavigationLink(destination: LandmarkList()) {
                    Text("查看所有地标")
                }
                
            }
            .navigationBarTitle(Text("精选"))
            .navigationBarItems(trailing:
                Button(action: { self.isPresented.toggle() }) {
                    Image(systemName: "person.crop.circle")
                        .imageScale(.large)
                        .padding()
                }
            ).sheet(isPresented: $isPresented, content: {
                Profile()
            })
        }
    }
}

struct Home_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            Home()
            // 暗黑模式
            Home().environment(\.colorScheme, .dark)
            // 辅助模式 大字体
            Home().environment(\.sizeCategory, .accessibilityExtraLarge)
        }
    }
}
